﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orion.Models;


namespace Orion.Controllers
{
    public class HomeController : Controller
    {
		//connects to repository
    	private NewsArticleRepositry m_newsArticleRep = new NewsArticleRepositry( );
 		private AdvertiesRepository m_advertiseRep = new AdvertiesRepository( );
		private EventRepository m_eventRep = new EventRepository( );
		private UserRepository m_userRep = new UserRepository( );

	
        public ActionResult Index( )
        {
			//poco class for news articles, adverties and events.
			var model = new HomeIndexViewModel( );

            //Getting newest news / article and then next four articles / news
            model.MainNewsArticle = (from n in m_newsArticleRep.GetAllNewsArticles()
                                     where n.Valid == true & ( n.ContentType == 1 || n.ContentType == 2 )
                                     select n).FirstOrDefault();

            model.NewsArticle = (from n in m_newsArticleRep.GetAllNewsArticles()
                                 where n.Valid == true & ( n.ContentType == 1 || n.ContentType == 2 )
                                 select n).Skip(1).Take(4);

            //Getting latest four events that are visible to everyone
        	var temp = m_eventRep.GetAllEvents( );
        	model.Events = (from t in temp
        	                where t.UserGroup == "Allir"
        	                select t).Take(4);
			
            return View( model );
        }


		//[HttpGet]
		public ActionResult About( int? id )
		{
			//validation
			if ( ! id.HasValue )
			{
				return View("Error");
			}

			//Gets Information about Iris + staff info.

			var allNewsArticles = m_newsArticleRep.GetAllNewsArticles();

			var model = from a in allNewsArticles
						where a.Valid == true && a.NewsArticleType.NAType == "Iris"
						select a;

			return View( model );
		}
		[ValidateInput(false)]
		[HttpGet]
		public ActionResult Help( int? id )
		{
		    //validation
		    if ( ! id.HasValue )
		    {
		        return View("Error");
		    }

		    //all news fetched
			var allNewsArticles = m_newsArticleRep.GetAllNewsArticles( );

		    var model = from a in allNewsArticles
		                where a.Valid == true && a.NewsArticleType.NAType == "Hjálp"
		                select a;

		    return View( model );
		}
        
        public ActionResult Webtree( )
        {
            return View( );
        }

		public ActionResult Contact( )
		{
			var model = m_userRep.GetAllUsers( );

			return View( model );
		}

		public ActionResult Donate( )
		{
			return View( );
		}

		
		public static List<SelectListItem> GetAllPostCode( )
		{
			var postRep = new PostNumberRepository( );

			var postCode = postRep.GetAllPostCode( );

			List<SelectListItem> model = new List<SelectListItem>( );

			//all postnumber fetched and put into SelectList
			foreach ( var code in postCode )
			{
				model.Add( new SelectListItem
				          	{
				          		Text = string.Format( "{0} {1}", code.PostCode1, code.City ),
								Value = code.PostCode1.ToString()
				          	} );	
			}

			return model;
		}
    }
}


